package com.ruoyi.consumer.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.consumer.domain.CCar;
import com.ruoyi.consumer.domain.CCompany;
import com.ruoyi.consumer.service.ICCarService;
import com.ruoyi.consumer.service.ICCompanyService;
import com.ruoyi.system.service.ISysUserService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 车辆信息Controller
 * 
 * @author YFENTEST
 * @date 2023-12-05
 */
@RestController
@RequestMapping("/consumer/car")
public class CCarController extends BaseController
{
    @Autowired
    private ICCarService cCarService;

    @Autowired
    private ICCompanyService cCompanyService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询车辆信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CCar cCar)
    {
        Long userAdminId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userAdminId);
        Assert.notNull(sysUser, "用户不存在");

        String carCompany;
        if (userAdminId != 1) {
            CCompany company = new CCompany();
            company.setCompanDept(Math.toIntExact(sysUser.getDeptId()));
            List<CCompany> cCompany = cCompanyService.selectCCompanyList(company);
            if (cCompany.isEmpty()) {
                carCompany = "";
                return null;
            }else {
                cCar.setCompanyId(cCompany.get(0).getCompanyId());
                carCompany = cCompany.get(0).getCompanyName();
            }

        } else {
            carCompany = "";
        }
        startPage();
        List<CCar> list = cCarService.selectCCarList(cCar);
        list.forEach(item -> {
            item.setCreateBy(carCompany);
        });
        return getDataTable(list);
    }

    /**
     * 导出车辆信息列表
     */
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CCar cCar)
    {
        List<CCar> list = cCarService.selectCCarList(cCar);
        ExcelUtil<CCar> util = new ExcelUtil<CCar>(CCar.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    /**
     * 获取车辆信息详细信息
     */
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable("carId") Integer carId)
    {
        return success(cCarService.selectCCarByCarId(carId));
    }

    /**
     * 新增车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CCar cCar)
    {

            Long userAdminId = SecurityUtils.getUserId();
            SysUser sysUser = sysUserService.selectUserById(userAdminId);
            Assert.notNull(sysUser, "用户不存在");


                CCompany company = new CCompany();
                company.setCompanDept(Math.toIntExact(sysUser.getDeptId()));
                List<CCompany> cCompany = cCompanyService.selectCCompanyList(company);
                if (cCompany.isEmpty()) {
                    return null;
                }else {
                    cCar.setCompanyId(cCompany.get(0).getCompanyId());
                }


        return toAjax(cCarService.insertCCar(cCar));
    }

    /**
     * 修改车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CCar cCar)
    {
        return toAjax(cCarService.updateCCar(cCar));
    }

    /**
     * 删除车辆信息
     */
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{carIds}")
    public AjaxResult remove(@PathVariable Integer[] carIds)
    {
        return toAjax(cCarService.deleteCCarByCarIds(carIds));
    }
}
